﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace CodeSmackdown.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "ViewSnippet",
                "ViewSnippet/{snippetID}",
                new { controller = "Home", action = "ViewSnippet" }
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            // not sure this is the right thing to do.
            // TODO: verify this is the right thing to do, also make sure that we get the right / user info
            if (Request.IsAuthenticated && HttpContext.Current.Session.User() == null)
            {
                Models.CodeSmackdownDataContext ctx = new Models.CodeSmackdownDataContext();
                HttpContext.Current.Session.SetUser(ctx.UpdateLoginTime(this.User.Identity.Name));
            }
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}